import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Main 
{
	public static String fName = "file.txt";
	
	public static void main(String[] args) throws IOException
	{
		System.out.println("~~~~~~~~~~~~~~~~~~~Bracketo~~~~~~~~~~~~~~~~~~~");
		System.out.println("Formats your project, adds newlines before {s!");
		System.out.println("Enter a file or directory you wish to bracketo");
		Scanner scanner = new Scanner(System.in);
		String input = scanner.nextLine();
		File file = new File(input);
		if (file.isFile())
		{
			System.out.println("File detected. Bracketo-ing \"" + input + "\"...");
			int changes = bracketo(file);
			System.out.println("Done. I made " + changes + " changes.");
		}
		else if (file.isDirectory())
		{
			System.out.println("Directory detected. Bracketo files with what extension? (enter \".*\" for all extensions)");
			String input2 = scanner.nextLine();
			if (input2.charAt(0) != '.')
				input2 = "." + input2;
			if (input2.equals(".*"))
				System.out.println("Bracketo-ing all files in \"" + input + "\"...");
			else
				System.out.println("Bracketo-ing all files in \"" + input + "\" with extension \"" + input2 + "\"...");
			String outIndents = "\t";
			int totalChanges = exploreDirectory(file, input2, outIndents);
			System.out.println("Done. I made a total of " + totalChanges + " changes. Press enter to close...");
			scanner.nextLine();
		}
		else
		{
			System.out.println("\"" + input + "\" was not recognized as a file or a directory. Press enter to close...");
			scanner.nextLine();
		}
		scanner.close();
	}
		
	public static int bracketo(File file) throws IOException
	{
		int changes = 0;
		BufferedReader reader = new BufferedReader(new FileReader(file));
		ArrayList<String> lines = new ArrayList<String>();
		//read
		{
			String line;
			do
			{
				line = reader.readLine();
				if (line != null)
				{
					lines.add(line);
				}
			}
			while (line != null);
			reader.close();
		}
		BufferedWriter writer = new BufferedWriter(new FileWriter(file));
		//modify and write
		{
			for (String line : lines)
			{
				String curIndent = "";
				boolean indenting = true;
				char curChar;
				String outputLine = "";
				boolean isComment = false;
				for (int index = 0; index < line.length(); index++)
				{
					boolean firstNonWhitespace = false;
					curChar = line.charAt(index);
					if (index >= 1)
						if (line.charAt(index - 1) == '/' && curChar == '/')
							isComment = true;
					if (indenting)
					{
						if (curChar == ' ' || curChar == '\t')
							curIndent += curChar;
						else
						{
							indenting = false;
							firstNonWhitespace = true;
						}
					}
					
					if (!firstNonWhitespace && !isComment && curChar == '{')
					{
						outputLine += "\n" + curIndent;
						changes++;
					}
					outputLine += curChar;
				}
				writer.write(outputLine + "\n");
			}
			writer.close();
		}
		return changes;
	}
	
	public static String getExtension(File file)
	{
		String name = file.getName();
		return name.substring(name.indexOf("."));
	}
	
	public static int exploreDirectory(File file, String ext, String outIndents) throws IOException
	{
		int totalChanges = 0;
		for (File child : file.listFiles())
		{
			if (child.isDirectory())
			{
				System.out.println(outIndents +"Entering subdirectory \"" + child.getName() + "\"...");
				exploreDirectory(child, ext, outIndents + "\t");
				System.out.println(outIndents + "Done.");
			}
			else
			{
				if(getExtension(child).equals(ext) || ext.equals(".*"))
				{
					System.out.println(outIndents + "Bracketo-ing \"" + child.getName() + "\"...");
					int changes = bracketo(child);
					System.out.println(outIndents +"Done. I made " + changes + " changes.");
					totalChanges += changes;
				}
			}
		}
		return totalChanges;
	}
}
